//
//  PersonalCenterViewController.m
//  atXiaoGeBClient
//
//  Created by Linyu Niu on 23/04/2018.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "PersonalCenterViewController.h"

@interface PersonalCenterViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * personalTableView;
}
@end

@implementation PersonalCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView * bgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_background"]];
    bgView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:bgView];
    __weak PersonalCenterViewController * weakSelf = self;
    UIView * navView = [[CTMediator sharedInstance] navgationHeadViewInTargetView:self.view params:[NSDictionary dictionaryWithObjectsAndKeys:@"个人中心",@"title", nil] leftAction:^(UIButton * sender){
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } rightAction:nil];
    navView.backgroundColor = [UIColor clearColor];
    personalTableView = [self addTableViewInView:self.view frame:CGRectMake(0, navView.frame.origin.y + navView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - (navView.frame.origin.y + navView.frame.size.height)) delegateAndDataSource:self];
//    personalTableView.scrollEnabled = NO;
    personalTableView.backgroundColor = [UIColor clearColor];//[UIColor colorWithRed:233.0 / 255.0 green:233.0 / 255.0 blue:233.0 / 255.0 alpha:1.0];
}
-(void)createSegmentcontrolButton:(UIView *)view{
    CGRect frame = CGRectMake(0, 0, view.frame.size.width / 3.0, 80);
    
    for (NSInteger i = 0; i < 3; i ++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = frame;
        [view addSubview:button];

        UILabel * valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, button.frame.size.width, (button.frame.size.height - 20)/ 2)];
        valueLabel.font = [UIFont systemFontOfSize:17];
        valueLabel.textAlignment = NSTextAlignmentCenter;
        valueLabel.textColor = [UIColor whiteColor];
        [button addSubview:valueLabel];
        
        UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, valueLabel.frame.size.height + valueLabel.frame.origin.y, button.frame.size.width, valueLabel.frame.size.height)];
        nameLabel.font = [UIFont systemFontOfSize:17];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.textColor = UIColorFromRGB(0x7A99AF);
        [button addSubview:nameLabel];
        
        frame.origin.x += frame.size.width;
        
#if kDevelopment
        if (i == 0) {
            valueLabel.text = @"90%";
            nameLabel.text = @"昨日取件率";
        }else if(i == 1){
            valueLabel.text = @"99元";
            nameLabel.text = @"今日收入";
        }else if(i == 2){
            valueLabel.text = @"18单";
            nameLabel.text = @"今日完成";
        }
#endif
    }
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, frame.size.height);
}

-(void)changePermission:(UISwitch *)s{
    UIViewController * controller = [[CTMediator sharedInstance] assignController];
    [self.navigationController pushViewController:controller animated:YES];
}
-(void)createHeadCellInView:(UIView *)view{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(20, 20, 60, 60);
    imageView.backgroundColor = [UIColor blueColor];
    imageView.layer.cornerRadius = imageView.frame.size.width / 2.0;
    [view addSubview:imageView];
    
    UILabel * nameLabel = [[UILabel alloc] init];
    nameLabel.frame = CGRectMake(imageView.frame.origin.x + imageView.frame.size.width + 10,imageView.frame.origin.y - 3, 0, 20);
    
    nameLabel.font = [UIFont systemFontOfSize:14];
    nameLabel.textColor = [UIColor whiteColor];
    [view addSubview:nameLabel];
    
    UILabel * expressLabel = [[UILabel alloc] init];
    expressLabel.frame = CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.origin.y + nameLabel.frame.size.height, 0, nameLabel.frame.size.height);
    
    expressLabel.font = [UIFont systemFontOfSize:14];
    expressLabel.textColor = [UIColor whiteColor];
    [view addSubview:expressLabel];
    
    UILabel * scoreLabel = [[UILabel alloc] init];
    scoreLabel.frame = CGRectMake(nameLabel.frame.origin.x, expressLabel.frame.origin.y + expressLabel.frame.size.height, 0, nameLabel.frame.size.height);
    
    scoreLabel.font = [UIFont systemFontOfSize:14];
    scoreLabel.textColor = [UIColor whiteColor];
    [view addSubview:scoreLabel];
    
    
#if kDevelopment
    expressLabel.text = @"EMS/韵达/百世";
    nameLabel.text = @"张三丰 ";
    scoreLabel.text = @"综合评分：";
#endif
    [nameLabel enlargeWidthToKeepFontSize];
    [expressLabel enlargeWidthToKeepFontSize];
    [scoreLabel enlargeWidthToKeepFontSize];
    
    NSInteger score = 3;
    NSInteger totalStar = 0;
    
    
    CGRect frame = CGRectMake(scoreLabel.frame.origin.x + scoreLabel.frame.size.width, scoreLabel.frame.origin.y, 10, scoreLabel.frame.size.height);
    for (NSInteger i = 0;i < score; i++) {
        UIImageView * imgView = [[UIImageView alloc]initWithFrame:frame];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.image = [UIImage imageNamed:@"star_selected"];
        [view addSubview:imgView];
        
        frame.origin.x += frame.size.width + 3;
        totalStar ++;
    }
    
    
    for (NSInteger j = totalStar; j < 5; j ++) {
        UIImageView * imgView = [[UIImageView alloc]initWithFrame:frame];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.image = [UIImage imageNamed:@"StarLevelOff"];
        [view addSubview:imgView];
        frame.origin.x += frame.size.width + 3;
    }

    
    UILabel * scoreValueLabel = [[UILabel alloc] init];
    scoreValueLabel.frame = CGRectMake(frame.origin.x + 10, scoreLabel.frame.origin.y, 45, scoreLabel.frame.size.height);
    scoreValueLabel.backgroundColor = [UIColor colorWithRed:32.0/255.0 green:198.0/255.0 blue:122.0/255.0 alpha:1.0];
    scoreValueLabel.font = [UIFont systemFontOfSize:10.0];
    scoreValueLabel.textColor = [UIColor whiteColor];
    scoreValueLabel.layer.cornerRadius = 10.0;
    scoreValueLabel.layer.masksToBounds = YES;
    scoreValueLabel.textAlignment = NSTextAlignmentCenter;
    scoreValueLabel.text = [NSString stringWithFormat:@"%@.0",[NSNumber numberWithInteger:score]];
    [view addSubview:scoreValueLabel];
    
    
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, imageView.frame.size.height + imageView.frame.origin.y * 2);
}

#pragma mark-TableView


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = [[ElementTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.backgroundColor = [UIColor clearColor];
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(20, 0, tableView.frame.size.width - 40, 0)];
    bgView.backgroundColor = [UIColor colorWithRed:44.0 / 255.0 green:75.0 / 255.0 blue:96.0 / 255.0 alpha:1.0];
    
    if([indexPath section] == 0){
        [self createHeadCellInView:bgView];
    }else if([indexPath section] == 1){
        [self createSegmentcontrolButton:bgView];
    }else {
        
        UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, tableView.frame.size.width, 30)];
        nameLabel.font = [UIFont systemFontOfSize:16];
        nameLabel.textColor = [UIColor whiteColor];
        [bgView addSubview:nameLabel];
        bgView.frame = CGRectMake(bgView.frame.origin.x, bgView.frame.origin.y, bgView.frame.size.width, nameLabel.frame.size.height + nameLabel.frame.origin.y * 2);
        if([indexPath section] == 2){
            if ([indexPath row] == 0) {
                nameLabel.text = @"派单权限";
                UISwitch * s = [[UISwitch alloc]initWithFrame:CGRectMake(bgView.frame.size.width - 60, (bgView.frame.size.height - 20) / 2, 50, 20)];
                [s addTarget:self action:@selector(changePermission:) forControlEvents:UIControlEventValueChanged];
                [bgView addSubview:s];
            }else if([indexPath row] == 1){
                nameLabel.text = @"我的钱包";
                
            }else if([indexPath row] == 2){
                nameLabel.text = @"面对面接单";
            }
            
            if ([indexPath row] == 1 || [indexPath row] == 2) {
                UIImageView * arrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iconPDArraw"]];
                [bgView addSubview:arrow];
                arrow.frame = CGRectMake(bgView.frame.size.width - arrow.image.size.width - 10, (bgView.frame.size.height - arrow.image.size.height) / 2, arrow.image.size.width, arrow.image.size.height);
            }
        }else if([indexPath section] == 3){
            if ([indexPath row] == 0) {
                nameLabel.text = @"联系客服";
            }else if([indexPath row] == 1){
                nameLabel.text = @"小哥电台";
            }
        }
    }
    
    
    NSInteger numberRow = [tableView numberOfRowsInSection:[indexPath section]];
    if (numberRow == 1) {
        bgView.layer.cornerRadius = 5.0;
    }else{
        
        if ([indexPath row] == 0) {
            
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(5, 5)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = bgView.bounds;
            maskLayer.path = maskPath.CGPath;
            bgView.layer.mask = maskLayer;
        }else if([indexPath row] == numberRow - 1){
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = bgView.bounds;
            maskLayer.path = maskPath.CGPath;
            bgView.layer.mask = maskLayer;
        }
    }
    
    if ([indexPath row] < numberRow - 1) {
        UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, bgView.frame.size.height - 1.0, bgView.frame.size.width, 0.5)];
        lineView.backgroundColor = UIColorFromRGB(0x255272);
        [bgView addSubview:lineView];
    }
    cell.cellHeight = bgView.frame.size.height + bgView.frame.origin.y;
    
    [cell addSubview:bgView];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = (ElementTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 15)];
    return v;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0 || section == 1) {
        return 1;
    }else if(section == 2){
        return 3;
    }else if(section == 3){
        return 2;
    }
    
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([indexPath section] == 2) {
        if ([indexPath row] == 1) {
            UIViewController * controller = [[CTMediator sharedInstance] myWalletController];
            [self.navigationController pushViewController:controller animated:YES];
        }
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 6;
}
@end
